package com.yunfeisoft.business.controller;

import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.Receipt;
import com.yunfeisoft.business.service.inter.ElderService;
import com.yunfeisoft.business.service.inter.ReceiptService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: ReceiptController
 * Description: 收款单Controller
 * Author: Jackie liu
 * Date: 2020-04-01
 */
@Controller
public class ReceiptController extends BaseController {

    @Autowired
    private ReceiptService receiptService;
    @Autowired
    private ElderService elderService;

    /**
     * 添加收款单
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receipt/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(Receipt record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "elderId", "老人为空");
        validator.required(request, "beginDate", "开始日期为空");
        validator.required(request, "endDate", "结束日期为空");
        validator.required(request, "totalAmount", "总费用为空");
        validator.required(request, "payType", "付款方式为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        record.setStatus(Receipt.ReceiptStatusEnum.WAITING.getValue());
        receiptService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改收款单
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receipt/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(Receipt record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "elderId", "老人为空");
        validator.required(request, "beginDate", "开始日期为空");
        validator.required(request, "endDate", "结束日期为空");
        validator.required(request, "totalAmount", "总费用为空");
        validator.required(request, "payType", "付款方式为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        receiptService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询收款单
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receipt/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        Receipt record = receiptService.load(id);
        if (record != null) {
            Elder elder = elderService.load(record.getElderId());
            record.setElderName(elder.getName());
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询收款单
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receipt/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String elderName = ServletRequestUtils.getStringParameter(request, "elderName", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("elderName", elderName);

        Page<Receipt> page = receiptService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除收款单
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receipt/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        receiptService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 入账
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/receipt/accept", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response accept(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        receiptService.modifyStatus(id, Receipt.ReceiptStatusEnum.ACCEPT.getValue());
        return ResponseUtils.success("入账成功");
    }
}
